﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TestWebClick
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            //HtmlElementCollection hrefs = webBrowser1.Document.GetElementsByTagName("works");
            //for (int i = 0; i < hrefs.Count; i++)//hrefs.Count
            //{
            //    if (i == 20)
            //    {
            //        hrefs[i].InvokeMember("click");
            //    }
            //}
            //HtmlElement htmlElement = webBrowser1.Document.GetElementById("works");
            //htmlElement.InnerText = tbUser.Text.ToString().Trim();//fill name
            //for (int i = 0; i < webBrowser1.Document.Forms.Count; i++)
            //{
            //    if (webBrowser1.Document.Forms[i].Name == "form2")
            //    {

            //    }
            //}
          
            //HtmlElementCollection radio = webBrowser1.Document.Forms["form2"].GetElementsByTagName("input");

            //radio[0].InvokeMember("click");//选择radio控件

            //HtmlElement form2 = webBrowser1.Document.Forms["form2"];
            //form2.InvokeMember("submit");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("http://pb.hner.cn/sspt/allworks.aspx");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Process[] ps = Process.GetProcesses();
            
            foreach (Process p in ps)
            {
                try
                {
                    if (p.ProcessName == "iexplore") p.Kill();
                }
                catch (Exception)
                {
                    break;
                }
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlElement works = webBrowser1.Document.GetElementById("works");
            if (works != null)
            {
                works.InnerText = "五彩池";//fill name
            }

            webBrowser1.Document.InvokeScript("GetWorksCount", new object[] { 0 });
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            HtmlElementCollection hrefs = webBrowser1.Document.GetElementsByTagName("a");
            for (int i = 0; i < hrefs.Count; i++)//hrefs.Count
            {//hbqbjiliping
                HtmlElement element = hrefs[i];
                if (element.InnerText == "五彩池")
                {
                    element.InvokeMember("click");
                    //string url = element.OuterHtml.Split('\"')[1];
                    //this.webBrowser1.Url = new Uri(url);
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            timer2.Enabled = checkBox1.Checked;

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            timer2.Interval = (int)numericUpDown1.Value;
        }
    }
}
